/**
 * Created by asus on 2017/5/2.
 */


$(function(){
    var isIE = function(ver){
        var b = document.createElement('b');
        b.innerHTML = '<!--[if IE ' + ver + ']><i></i><![endif]-->';
        return b.getElementsByTagName('i').length === 1
    };
    if(isIE(6)||isIE(7)||isIE(8)||isIE(9)){
        window.location.href = urlg;
        //alert("当前版本不支持播放视频，请下载更高版本");
    }

    var tab_btn = $(".lg_rg_wra .tab_wra span");
    var download_btn = $(".download");
    var verify_code_wra = $(".verify_code_wra");
    var img_code_verify = function(){

        var login_flag = $.cookie("exist", {
            name: 'login_flag'
        });
        if(login_flag){
            verify_code_wra.show();
        }else {
            verify_code_wra.hide();
        }

        return login_flag;
    };
    var img_code_verify_flag = img_code_verify();

    tab_btn.on("click",function(){
        $(this).addClass("active").siblings("span").removeClass("active");
        var flag = $(this).index();
        var register_wra = $(".register_wra");
        var login_wra = $(".login_wra");
        if(flag==0){
            //登录
            download_btn.show();
            register_wra.hide();
            login_wra.show();
        }else if(flag==1){
            //注册
            download_btn.hide();
            register_wra.show();
            login_wra.hide();
        }
    });

    //忘记密码 重置密码
    $("#pw_find_btn").on("click",function(){
        $(".password_reset_wrap").show();
        $(".lg_rg_wra").hide();
        $(".download").hide();
    });
    $("#login_tab").on('click',function(){
        $(".password_reset_wrap").hide();
        $(".lg_rg_wra").show();
        $(".download").show();
    });
    var validateElement = {
        onkeyup: false,
        onclick: false,
        onfocusout: false,
        showErrors: function (errorMap, errorList) {
            if (errorList.length > 0) {
                alert(errorList[0].message);
            }
        },
        rules: {
            mobilePhone: {required: true,mobile:true},
            password: {required: true},
            verify_code:{required: true}
        },
        messages: {
            mobilePhone: {required: "请输入手机号码",mobile:"手机号格式有误"},
            password: {required: "请输入密码"},
            verify_code:{required: "请输入验证码"}
        }
    };


    var re_validateElement = {
        onkeyup: false,
        onclick: false,
        onfocusout: false,
        showErrors: function (errorMap, errorList) {
            if (errorList.length > 0) {
                alert(errorList[0].message);
            }
        },
        rules: {
            mobilePhone: {required: true,mobile:true},
            note_code:{required: true},
            r_password: {required: true},
            repassword: {required: true}
        },
        messages: {
            mobilePhone: {required: "手机号码不能为空",mobile:"手机号格式有误"},
            note_code: {required: "短信验证码不能为空"},
            r_password:{required: "密码不能为空"},
            repassword:{required: "确认密码不能为空",equalTo:"两次输入密码不一致"}
        }
    };

    //手机号登录
    $("#login_btn").on("click",function(){
        //var re = /^1\d{10}$/;
        var login_info = $(".login_wra");
        login_info.validate(validateElement);
        if (!login_info.valid()) {
            if(!img_code_verify_flag){
                $.cookie("set", {
                    duration: 365,
                    name: 'login_flag',
                    value: 0
                });
                img_code_verify_flag = img_code_verify();
            }
            return false;
        }
        //图片验证码是否正确
        var img_code_check = function(){
            var img_flag = true;
            if($(".verify_code:visible")[0]){
                $.ajax({
                    type: "post",
                    url: urlv,
                    datatype: "JSON",
                    async:false,
                    data: {
                        verify:$(".verify_code").val(),
                        type:0,
                        device:''
                    },
                    success: function (data) {
                        data= $.parseJSON(data);
                        if(data.code==0){
                            img_flag=false;
                            $("#img-verify").attr("src",'/captcha/new.html?time='+Math.random());

                            alert(data.data);

                            return false;
                        }else if(data.code==1){
                            img_flag=true;
                        }
                    }
                });
                return img_flag;
            }
            return img_flag;
        };
        var img_code_flg = img_code_check();
        if(!img_code_flg){
            return false;
        }

        layui.use('layer', function(){
            var layer = layui.layer;
            layer.ready(function(){
                var load_index = layer.load(0,{shade: [0.2, '#333']});
                $.ajax({
                    type: "post",
                    url: urlp,
                    datatype: "JSON",
                    data: {
                        url:'Login/signIn',
                        data: {
                            phone:$(".login_wra .mobilePhone").val(),
                            password:$(".login_wra .password").val(),
                            type:0,
                            device:""
                        }
                    },
                    success: function (obj) {
                        /* var result = obj;
                         if(!typeof(obj) == "object"){
                         result = $.parseJSON(obj);
                         }
                         console.log(result);*/
                        console.log(obj);
                        var result_txt = obj;
                        var result =  $.parseJSON(result_txt);
                        if(result.code==200){
                            $.cookie("set", {
                                duration: 365,
                                name: 'login',
                                value: result_txt
                            });
                            //经验值和余额
                            localStorage.u_nickname = result.data.u_nickname;
                            localStorage.u_avatar = result.data.u_avatar;
                            localStorage.u_score = result.data.u_score;
                            localStorage.u_balance = result.data.u_balance.replace(/,/g,"")*100/100;
                            localStorage.udid = result.data.udid;
                            localStorage.token = result.token;
                            localStorage.u_type = result.data.u_type;
                            localStorage.tim = result.data.tim;
                            localStorage.level = result.data.level;
                            //window.location.href = urlf;
                            if(result.data.u_type == 100){
                             window.location.href = urls;
                             }else{
                             window.location.href = urlf;
                             }
                        }else {
                            //设置是否需要登录时有图片验证码
                            console.log(img_code_verify_flag);
                            if(!img_code_verify_flag){
                                $.cookie("set", {
                                    duration: 365,
                                    name: 'login_flag',
                                    value: 0
                                });
                            }
                            alert(result.data.message);
                        }
                        layer.close(load_index);
                    },
                    error: function () {
                        console.log("error");
                    }
                });
            });
        });
    });

    //var get_code_show = function(){
        var get_code_btn = $(".get_code_btn");
        //获取验证码倒计时
        var setCountVal = function(that){
            var count_down = 60;
            var setVal = function(){
                if(count_down==0){
                    //count_down = 60;
                    $(that).removeClass("disabled").html("获取验证码");
                    return false;
                }else {
                    $(that).addClass("disabled");
                    $(that).html("重新发送("+(count_down--)+")")
                }
                setTimeout(function(){
                    setVal();
                },1000);

            };
            setVal();
        };
        //获取验证码
        get_code_btn.on("click",function(){
            var get_code_btn_current = $(this);
            if($(this).hasClass("disabled")){
                return false;
            }

            //reset_wra
            var form_now = $(this).parent("form");
            var flag = form_now.validate(re_validateElement).element(form_now.children(".mobilePhone"));
            if(!flag){
                return false;
            }
            $.ajax({
                type: "post",
                url: urlp,
                datatype: "JSON",
                data: {
                    url:'Login/getVerifyCode',
                    data: {
                        phone: get_code_btn_current.siblings(".mobilePhone").val()
                    }
                },
                success: function (obj) {
                    var result =  $.parseJSON(obj);
                    if(result.code==200){
                        setCountVal(get_code_btn_current);
                    }
                },
                error: function () {
                    console.log("error");
                }
            });
        });
    //};


    //注册
    $("#register_btn").on("click",function(){
        var register_info = $(".register_wra");
        //re_validateElement.rules.repassword.equalTo = ".register_wra .r_password";
        register_info.validate(re_validateElement);
        if (!register_info.valid()) {
            return false;
        }
        if($(".register_wra .r_password").val()!=$(".register_wra .repassword").val()){
            alert("两次输入密码不一致");
            return false;
        }

        $.ajax({
            type: "post",
            url: urlp,
            datatype: "JSON",
            data: {
                url:'Login/registPhone',
                data: {
                    phone:$(".register_wra .mobilePhone").val(),
                    code:$(".register_wra .note_code").val(),
                    password:$(".register_wra .r_password").val()
                }
            },
            success: function (obj) {
                var result =  $.parseJSON(obj);
                if(result.code!=200){
                    alert(result.data.message);
                }else {
                    alert("注册成功");
                    $(".tab_wra").find("span").eq(0).trigger("click");
                }
            },
            error: function () {
                console.log("error");
            }
        });
    });


    //重置密码
    $("#reset_btn").on("click",function(){
        var reset_info = $(".reset_wra");
        //re_validateElement.rules.repassword.equalTo = ".reset_wra .r_password";
        reset_info.validate(re_validateElement);
        if (!reset_info.valid()) {
            return false;
        }
        if($(".reset_wra .r_password").val()!=$(".reset_wra .repassword").val()){
            alert("两次输入密码不一致");
            return false;
        }
        $.ajax({
            type: "post",
            url: urlp,
            datatype: "JSON",
            data: {
                url:'Login/setPassword',
                data: {
                    phone:$(".reset_wra .mobilePhone").val(),
                    code:$(".reset_wra .note_code").val(),
                    password:$(".reset_wra .r_password").val()
                }
            },
            success: function (obj) {
                var result =  $.parseJSON(obj);
                if(result.code!=200){
                    alert(result.data.message);
                }else {
                    alert("重置密码成功");
                    $("#login_tab").trigger("click");
                    $(".tab_wra").find("span").eq(0).trigger("click");
                }
            },
            error: function () {
                console.log("error");
            }
        });
    });

});
























